<template>
  <div class="app-container">
    <wise-crud ref="wise-crud" :urls="urls" :fields="fields" :handle="handle" >
      <template v-slot:handle="slotProps">
        <!-- <el-button type="warning" size="small" class="activeBtn" round
          @click.native.prevent="initHandle(slotProps.row)">计划审批</el-button> -->
        
          <el-button type="info" size="small" round @click.native.prevent="viewHandle(slotProps.row)">查看</el-button>
          <template v-if="slotProps.row.sp==0">

          <el-button type="primary" size="small" class="activeBtn" round
            @click.native.prevent="throughHandle(slotProps.row)">
            通过
          </el-button>
          <el-button type="danger" size="small" class="activeBtn" round
            @click.native.prevent="rejectedHandle(slotProps.row)">
            驳回
          </el-button>
        </template>
      </template>
    </wise-crud>
    <el-dialog ref="viewDrawer" title="查看计划" :visible.sync="viewDrawer" destroy-on-close>
      <div v-if="viewDrawer" class="draw-body">
        <div class="wise-form">
          <el-form :validate-on-rule-change="false" label-width="130px" label-suffix=":">
            <el-form-item label="计划名称" prop="problemTypeName">  {{ object.item_name }} </el-form-item>
            <el-form-item label="属地负责单位" prop="problemTypeName">  {{ object.item_dwfzdw }} </el-form-item>
            <el-form-item label="项目种类" prop="problemTypeName">   
              {{initiateStatusHandle( object.item_type )}}
            </el-form-item>
          
            <el-form-item label="项目类型" prop="problemTypeName">  {{initiateStatusHandle1( object.item_lx )}} </el-form-item>
            <el-form-item label="动火作业内容" prop="problemTypeName">  {{ object.item_dhzynr }} </el-form-item>
            <el-form-item label="属地负责单位" prop="problemTypeName">  {{ object.item_dwfzdw }} </el-form-item>
            <el-form-item label="设计管径" prop="problemTypeName">  {{ object.item_sjgj }} </el-form-item>
            <el-form-item label="计划作业时间" prop="problemTypeName">  {{ object.item_jhzysj1 }} - {{ object.item_jhzysj2 }}</el-form-item>
            <el-form-item label="作业时间" prop="problemTypeName">  {{ object.item_jhzysj }} </el-form-item>
            <el-form-item label="计划施工队伍" prop="problemTypeName">  {{ object.item_jhsgdw }} </el-form-item>
            <el-form-item label="工作量" prop="problemTypeName">  {{ object.item_gzl }} </el-form-item>
            <el-form-item label="作业位置" prop="problemTypeName">  {{ object.item_weizhi }} </el-form-item>
            <el-form-item label="备注" prop="problemTypeName">  {{ object.notes }} </el-form-item>
            <!-- <el-form-item label="经纬度" prop="problemTypeName">  精度：{{ object.wz_jing }}纬度：{{object. wz_wei }}</el-form-item> -->
             
            <el-form-item label="创建人" prop="problemTypeName">  {{ object.cname }} </el-form-item>
            <!-- <el-form-item label="创建时间"  type="date"  value-format="yyyy-MM-dd HH:mm:ss" prop="ct">  {{ object.ct }} </el-form-item> -->
            <el-form-item label="审批状态" prop="problemTypeName"> 
              {{initiateStatusHandle2( object.sp )}}  
             </el-form-item>
            <el-form-item v-if="object.sp>0||object.sp<0" label="审批人" prop="problemTypeName">  {{ object.sprname }} </el-form-item>
            <el-form-item v-if="object.sp<0||object.sp<0" label="审批意见" prop="problemTypeName">  {{ object.spbz }} </el-form-item>
           
         
          </el-form>
        </div>
      </div>
    </el-dialog>
    <el-dialog title="驳回" :visible.sync="rejectedDialog" :close-on-click-modal="false" close-on-press-escape
      destroy-on-close width="30%" @close="rejectedDialogClosed">
      <el-form ref="rejectedForm" :model="rejectedObj" :rules="rules" label-width="100px">
        <el-form-item label="驳回原因" prop="spbz">
          <el-input v-model="rejectedObj.spbz" type="textarea" :rows="5" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="rejectedDialog = false">取 消</el-button>
        <el-button type="primary" @click="rejectedSubmit">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog title="通过" :visible.sync="rejectedDialogx" :close-on-click-modal="false" close-on-press-escape
      destroy-on-close width="30%" @close="rejectedDialogClosed">
      <el-form ref="rejectedFormx" :model="rejectedObj" :rules="rules" label-width="100px">
        <el-form-item label="意见" prop="spbz">
          <el-input v-model="rejectedObj.spbz" type="textarea" :rows="5" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="rejectedDialogx = false">取 消</el-button>
        <el-button type="primary" @click="rejectedSubmitx">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import WiseCrud from '@/components/Crud/wiseCrud'
  import WiseFile from '@/components/Crud/components/wiseFile'
  import {
    postHandle
  } from '../../../../api'
  import checkPermission from '../../utils/permission' // 权限判断函数
  export default {
    name: 'UserManage',
    components: {
      WiseCrud
    },
    data() {
      return {
        rejectedDialogx:false,
        urls: {
          list: '/api/v1/welder-sbtx-second/project-plan/list',
          add: '/api/v1/welder-sbtx-second/project-plan/ppAdd',
          show: '/api/v1/welder-sbtx-second/project-plan/show/',
          update1: '/api/v1/welder-sbtx-second/project-plan/update1/',
          update2: '/api/v1/welder-sbtx-second/project-plan/update2/',
          exportytg: '/api/v1/welder-sbtx-second/project-plan/exportytg',
        },
        fields: [{
            label: '序号',
            type: 'index',
            use: ['table']
          },
          {
            label: '计划名',
            prop: 'item_name',
            type: 'textarea',
            use: ['table', 'add', 'view', 'edit', 'search']
          },
          {
            label: '管线名称',
            prop: 'item_guanxian',
            type: 'textarea',
            use: ['table', 'add', 'view', 'edit', 'search']
          },
          {
            label: '属地负责单位',
            prop: 'item_dwfzdw',
            type: 'textarea',
            use: ['table', 'add', 'view', 'edit', 'search']
          },
          {
            label: '计划时间',
            prop: 'item_jhzysj',
            type: 'textarea',
            use: ['table', 'add', 'view', 'edit', 'search']
          },{
            label: '审批状态',
            prop: 'manage',
            type: 'render',
            labelWidth: '130px',
            tableRender: (h, row, index) => {
              let value
              switch (row.sp) {
              case 0:
                value = '未审批'
                break
              case 1:
                value = '同意'
                break
              case -1:
                value = '不同意'
                break
              case 2:
                value = '已创建'
                break
              default:
                value = '类型错误'
            }
              return h('el-row',  value)
            },
           
            use: ['table', 'view']
          },
          {
            label: '分公司',
            prop: 'entSpaceName',
            type: 'textarea',
            use: ['table', 'add', 'view', 'edit', ]
          },
          {
            label: '所属企业',
            prop: 'item_dwfzdw',
            type: 'textarea',
            use: ['table', 'add', 'view', 'edit', ]
          },

          {
            label: '项目种类',
            prop: 'manage',
            type: 'render',
            labelWidth: '130px',
            tableRender: (h, row, index) => {
              let value
              switch (row.item_lx) {
              case '1':
                value = '投资'
                break
              case '2':
                value = '维修'
                break
             
              default:
                value = '类型错误'
            }
              return h('el-row',  value)
            },
           
            use: ['table', 'view']
          },
,
          {
            label: '动火作业内容',
            prop: 'item_dhzynr',
            type: 'textarea',
            placeholder: '请输入简介',
            use: ['add', 'view', 'edit']
          },
          {
            label: '计划作业时间1',
            prop: 'item_jhzysj1',
            type: 'textarea',
            placeholder: '请输入简介',
            use: ['add', 'view', 'edit']
          },
          {
            label: '计划施工队伍',
            prop: 'item_jhsgdw',
            type: 'textarea',
            placeholder: '请输入简介',
            use: ['add', 'view', 'edit']
          },
          {
            label: '工作量',
            prop: 'item_gzl',
            type: 'textarea',
            placeholder: '请输入简介',
            use: ['add', 'view', 'edit']
          },
          {
            label: '创建人',
            prop: 'cname',
            type: 'textarea',
            placeholder: '请输入简介',
            use: ['add', 'view', 'edit']
          },
        
         
          {
            label: '作业位置',
            prop: 'item_weizhi',
            type: 'textarea',
            placeholder: '请输入简介',
            use: ['add', 'view', 'edit']
          },
          {
            label: '经纬度',
            prop: 'wz_jing',
            type: 'textarea',
            placeholder: '请输入简介',
            use: ['add', 'view', 'edit']
          },
          {
            label: '创建时间',
            prop: 'ct',
            type: 'handle',
            use: ['textarea'],
            handle: [ 'edit', 'view', 'delete'],
            tableWidth: 320
          } , {
            label: '审批状态',
            prop: 'sp',
            type: 'textarea',
            placeholder: '请输入简介',
            use: ['add', 'view', 'edit']
          }, {
            label: '审批人',
            prop: 'spr',
            type: 'textarea',
            placeholder: '请输入简介',
            use: ['add', 'view', 'edit']
          }, {
            label: '审批意见',
            prop: 'spbz',
            type: 'textarea',
            placeholder: '请输入简介',
            use: ['add', 'view', 'edit']
          },{
            label: '操作',
            type: 'handle',
            use: ['table'],
            handle: [ ],
            tableWidth: 320
          }
        ],
        drawer: false,
        filterMethod(query, item) {
          return item.name.indexOf(query) > -1
        },

        userInfo: {},
        rejectedDialog: false,
        
        drawer1: false,
        drawer2: false,
        setting: {
          id: '',
          professionList: [],
          obj: {
            professionIds: []
          },
          rules: {}
        }   ,     object: {
          problemTypeName: null,
          depict: null,
          approvalUserId: null,
          name: null,
          imageUrl: ''
        },rejectedObj: {
          rejectReason: null
        } ,approvalData: [], viewDrawer: false, rules: {
          rejectReason: [{
            required: true,
            message: '必填项不能为空',
            trigger: 'blur'
          }]
        }
      }
    },
    computed: {
      handle() {
        if (this.checkPermission(['superSysOpsAdmin'])) {
          return ['search','exportytg']
        } else {
          return ['search']
        }
      }
    },
    methods: {
      checkPermission,
      initHandle(row) {
        this.$confirm('您确定要重置该用户的密码?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          })
          .then(() => {
            postHandle(
                '/api/v1/welder-uac-second/user-account-comb/init-password/' + row.userId, {}
              )
              .then((res) => {
                this.$message({
                  type: 'success',
                  message: '操作成功!'
                })
              })
              .catch(() => {
                this.$message({
                  type: 'error',
                  message: '操作失败'
                })
              })
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            })
          })
      },

      settingHandlea(row) {
        this.drawer1 = true
        this.setting.id = row.spaceMemberInfo ? row.spaceMemberInfo.id : ''
        if (row.professionList && row.professionList.length > 0) {
          this.setting.obj.professionIds = row.professionList.map(
            (item) => item.professionId
          )
        } else {
          this.setting.obj.professionIds = []
        }
        this.getJobList(
          row.entSpaceInfoList && row.entSpaceInfoList.length > 0 ?
          row.entSpaceInfoList[0].id :
          ''
        )
      },settingHandleb(row) {
        this.drawer2 = true
        this.setting.id = row.spaceMemberInfo ? row.spaceMemberInfo.id : ''
        if (row.professionList && row.professionList.length > 0) {
          this.setting.obj.professionIds = row.professionList.map(
            (item) => item.professionId
          )
        } else {
          this.setting.obj.professionIds = []
        }
        this.getJobList(
          row.entSpaceInfoList && row.entSpaceInfoList.length > 0 ?
          row.entSpaceInfoList[0].id :
          ''
        )
      },

      getJobList(id) {
        const req = {
          d: {
            filters: [{
                field: 'spaceId',
                operator: 'EQUALS',
                value: id
              },
              {
                field: 'level',
                operator: 'EQUALS',
                value: 1
              }
            ],
            fields: [],
            page: {
              p: 1,
              ps: 9999
            }
          }
        }
        postHandle('/api/v1/welder-uac-second/base-profession-comb/list', req).then(
          (response) => {
            this.setting.professionList = response.result
          }
        )
      },   viewHandle(row) {
        this.getObjectById(row.id)
        this.viewDrawer = true
      }, // 详情
      getObjectById(id) {
        postHandle(this.urls.show + id, {}).then(response => {
          this.object = response.result
          let data = response.result
          this.nullUserId = data.approvalUserId
          if (this.approvalData.findIndex(item => item.userId == data.approvalUserId) === -1) {
            this.isSubminBtn = true
            this.approvalData.push({
              userId: data.approvalUserId,
              username: "用户不存在",
              disabled: true
            })
          }
        })
      },
// 通过
throughHandle(row) {
        this.rejectedDialogx = true
        this.rejectedId = row.id


        // this.$confirm('您确定要审批通过吗?', '提示', {
        //   confirmButtonText: '确定',
        //   cancelButtonText: '取消',
        //   type: 'warning'
        // }).then(() => {
        //   postHandle(this.urls.update1 + id).then(res => {
        //     this.$message({
        //       type: 'success',
        //       message: '操作成功!'
        //     })
        //     this.$refs['wise-crud'].handleSearch(0)
        //   }).catch(() => {
        //     this.$message({
        //       type: 'error',
        //       message: '操作失败'
        //     })
        //   })
        // }).catch(() => {
        //   this.$message({
        //     type: 'info',
        //     message: '已取消'
        //   })
        // })
      },  // 驳回
      rejectedHandle(row) {
        this.rejectedId = row.id
        this.rejectedDialog = true

      },
      resetForm(formName) {
        this.$refs[formName].resetFields()
      },
      rejectedSubmitx() {
        this.$refs['rejectedFormx'].validate((valid) => {
          if (valid) {
            postHandle(this.urls.update1 + this.rejectedId, {
              obj: this.rejectedObj
            }).then(res => {
              this.$message({
                type: 'success',
                message: '操作成功!'
              })
              this.rejectedDialog = false
              this.$refs['wise-crud'].handleSearch(0)
           
            }).catch(() => {
              
              this.$message({
                  type: 'error',
                  message: '操作失败'
                })
            })
          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      rejectedSubmit() {
        this.$refs['rejectedForm'].validate((valid) => {
          if (valid) {
            postHandle(this.urls.update2 + this.rejectedId, {
              obj: this.rejectedObj
            }).then(res => {
              this.$message({
                type: 'success',
                message: '操作成功!'
              })
              this.rejectedDialog = false
              this.$refs['wise-crud'].handleSearch(0)
           
            }).catch(() => {
              
              this.$message({
                  type: 'error',
                  message: '操作失败'
                })
            })
          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      rejectedDialogClosed() {
        this.$refs['rejectedForm'].resetFields()
      },
      configureSuperUserRoleHandle(row, flag) {
        postHandle(
          `/api/v1/welder-uac-second/user-role-comb/configure-super-user-role/${row.userId}`, {
            d: {},
            obj: {
              authorization: flag
            }
          }
        ).then((res) => {
          this.$message('操作成功')
          this.$refs['wise-crud'].handleSearch()
        })
      }// 发起状态审批
      ,initiateStatusHandle(state) {
        let value
        switch (state) {
          case '632c38231768324e416ba907':
            value = '管体'
            break
          case '632c388c1768324e416ba908':
            value = '焊缝'
            break
          case '632c390f1768324e416ba909':
            value = '线改'
            break
          case '632c39361768324e416ba90a':
            value = '其他'
            break
          default:
            value = '类型错误'
        }
        return value
      }, initiateStatusHandle1(state) {
        let value
        switch (state) {
          case '1':
            value = '投资'
            break
          case '2':
            value = '维修'
            break
        
          default:
            value = '类型错误'
        }
        return value
      },initiateStatusHandle2(state) {
        let value
        switch (state) {
          case '0':
            value = '未审批'
            break
          case '1':
            value = '同意'
            break
          case '-1':
            value = '不同意'
            break
          default:
            value = '状态错误'
        }
        return value
      },
    }
  }
</script>

<style>
  .el-transfer .el-checkbox {
    display: block;
  }
</style>